var PoliceMap={
	mapBoss:null,
	DispatchMarker:null,
	circle:null,
	showSingle:false,
	init:function(mapBoss){
		var boss = this;
		boss.mapBoss = mapBoss;
	},
	renderLayer:function(){
		var boss = this;
		var mapBoss = boss.mapBoss;
		var zoom = mapBoss.map.getZoom();
		$("#listDivSwitch").show();
		if(boss.showSingle || zoom > 16){
			boss.renderSingle();
		}
		else if(zoom <= 16){
			boss.renderCenterLayer();
		}
		boss.renderPoliceList();
	},
	startDispatch:function(){
		var boss = this;
		if(boss.DispatchMarker){
			boss.mapBoss.map.removeOverlay(boss.DispatchMarker);
		}
		if(boss.circle){
			boss.mapBoss.map.removeOverlay(boss.circle);
		}
		boss.DispatchMarker = new BMap.Marker(new BMap.Point(113.906793,22.563063));
		boss.mapBoss.map.addOverlay(boss.DispatchMarker);
		PoliceMap.mapBoss.map.removeEventListener("mousemove",PoliceMap.moveDispatchMarker);
		PoliceMap.mapBoss.map.removeEventListener("click",PoliceMap.DispatchClick);
		boss.mapBoss.map.addEventListener("mousemove",boss.moveDispatchMarker);
		boss.mapBoss.map.addEventListener("click",boss.DispatchClick);
	},
	moveDispatchMarker:function(type){
		PoliceMap.DispatchMarker.setPosition(type.point);
	},
	DispatchClick:function(e){
		PoliceMap.circle = new BMap.Circle(e.point, 200);
		PoliceMap.mapBoss.map.addOverlay(PoliceMap.circle);
		PoliceMap.mapBoss.map.removeEventListener("mousemove",PoliceMap.moveDispatchMarker);
		PoliceMap.mapBoss.map.removeEventListener("click",PoliceMap.DispatchClick);
	},
	renderCenterLayer:function(){
		var boss = this;
		var mapBoss = boss.mapBoss;
		mapBoss.clearMapOverLay();
		var zoom = mapBoss.map.getZoom();
		var level;
		var pid = $("#xuanxiangka div[tag^='z'][selected]").last().attr("fullid");
		var dwlis = $("ul[tag='jl'] li.selected");
		var dwnums = "";
		for(var i=0;i<dwlis.length;i++){
			var dwnum = $(dwlis[i]).attr("tag");
			dwnums += dwnum+",";
		}
		if(zoom <=13 ){
			level = 1;
		}
		else if(zoom == 14){
			level = 2;
		}
		else if(zoom == 15){
			level = 3;
		}
		else if(zoom == 16){
			level = 4;
		}
		else if(level = 17){
			level = 5;
		}
		else if(level = 18){
			level = 6;
		}
		
		mapBoss.loadCenterLayer(burl+"/PolicePosition/getCenterCount.do",{"dwnum":dwnums,"id":pid,"level":level},"人",boss.showCenterWindow);
	},
	renderSingle:function(){
		var boss = this;
		var mapBoss = boss.mapBoss;
		mapBoss.clearMapOverLay();
		mapBoss.overlay.myOverlay = [];
		var pid = $("#xuanxiangka div[tag^='z'][selected]").last().attr("pid");
		if(pid == "1"){
			pid = "";
		}
		
		var dwlis = $("ul[tag='jl'] li.selected");
		var dwnums = "";
		for(var i=0;i<dwlis.length;i++){
			var dwnum = $(dwlis[i]).attr("tag");
			dwnums += dwnum+",";
		}
		
		sbl.ajax({
			dataType:'json',
			type:'get',
			data:{orgs:pid,dwnum:dwnums},
			url:burl+"/PolicePosition/getPoition.do",
			success:function(d){
				for(var j=0;j<d.length;j++){
					var row = d[j];
					var point = new BMap.Point(row.X,row.Y);
					var dwType;
					if(row.PERSONPROPERTY=="治安员"){
						dwType = "1";
					}
					else if(row.PERSONPROPERTY=="巡防员"){
						dwType = "2";
					}
					else if(row.PERSONPROPERTY=="保安"){
						dwType = "3";
					}
					else{
						dwType = "app";
					}
					var icon = new BMap.Icon(burl+"/images/map/team/"+dwType+".png", new BMap.Size(32,32));;
					var Marker = new BMap.Marker(point);
					Marker.data = row;						
					Marker.addEventListener("click",function(){
						boss.showPoliceInfoWindows(null,this);								
					});
					Marker.setIcon(icon);
					mapBoss.overlay.myOverlay.push(Marker);
					mapBoss.map.addOverlay(Marker);
				}
			}
		});
	},
	renderPoliceList:function(){
		var boss = this;
		var dwlis = $("ul[tag='jl'] li.selected");
		var dwnums = "";
		for(var i=0;i<dwlis.length;i++){
			var dwnum = $(dwlis[i]).attr("tag");
			dwnums += dwnum+",";
		}
		sbl.ajax({
			dataType:'json',
			type:'get',
			data:{dwnum:dwnums},
			url:burl+"/PolicePosition/getPositionsByOrgs.do",
			success:function(d){
				$("#list_div_context ul").empty();
				$(".map_list_title[dw]").hide();
				for(var j=0;j<d.length;j++){
					var row = d[j];
					var dw = row.dwnum;
					$(".map_list_title[dw='"+dw+"']").show();
					$("#list_div_context ul[dw='"+dw+"']").show();
					var li = $("<li></li>").html("<input type='checkbox'/>警号"+row.no).attr("x",row.x).attr("y",row.y).attr("no",row.no);
					$("#list_div_context ul[dw='"+dw+"']").append(li);
				}
				$("#list_div_context ul[dw] li").bind("dblclick",function(){
					var x = $(this).attr("x");
					var y = $(this).attr("y");
					var no = $(this).attr("no");
					boss.showPoliceInfoWindows(no);
				});
				
				$("#list_div_context ul[dw] li").bind("click",function(event){
					 event.stopPropagation();
				});
			}
		});
	},
	showCenterWindow:function(){
		var mapBoss = this.map.boss;
		var boss = mapBoss.Modules
		var center = new BMap.Point(this.point.lng,this.point.lat);
		var StatisticsModels = modelDao.getPoliceStatistics(this.data.ID);
		var allCount = 0;
		var htmlContent = "<table class='mapWindowsTable'>";
		for(var i=0;i<StatisticsModels.length;i++){
			var StatisticsModel = StatisticsModels[i];
			if(StatisticsModel.DWNUM){
				var label;
				if(StatisticsModel.DWNUM == "6"){
					label = "民警";
				}
				else{
					label = "第"+StatisticsModel.DWNUM+"支队伍";
				}
				htmlContent += "<tr>";
				htmlContent += "<td class='label'>"+label+":</td><td class='value'>"+StatisticsModel.COUNT+"人</td>";
				htmlContent += "</tr>";
				allCount += StatisticsModel.COUNT;
			}
		}
		htmlContent += "<tr>";
		htmlContent += "<td class='label'>总计:</td><td class='value'>"+allCount+"人</td>";
		htmlContent += "</tr>";
		htmlContent += "<tr>";
		htmlContent += "<td class='label'>配备终端数:</td><td class='value'>"+Math.ceil(Math.random()*allCount)+"个</td>";
		htmlContent += "</tr>";
		htmlContent += "<tr>";
		htmlContent += "<td class='label'>安装App数:</td><td class='value'>"+Math.ceil((Math.random()*allCount)-1)+"个</td>";
		htmlContent += "</tr>";
		htmlContent += "</table>";
		var InfoWindow = new BMap.InfoWindow(htmlContent);
		mapBoss.map.openInfoWindow(InfoWindow, center);
	},
	showPoliceInfoWindows:function(no,overlay){
		var boss = PoliceMap;
		var mapBoss = boss.mapBoss;
		if(!no){
			no = overlay.data.NO;
		}
		var model = null;
		if(overlay.data.PERSONPROPERTY == null || overlay.data.PERSONPROPERTY == ""){
			model = modelDao.getUserModel(overlay.data.NO);
			model.x = model.gpsX;
			model.y = model.gpsY;
			model.unitName = "";
			model.mobilephone = model.username;
			model.name = model.showname;
			model.no = model.id;
		}
		else{
			model = modelDao.getPoliceModel(no);
		}
		
		var point = new BMap.Point(model.x, model.y);
		var htmlContent = "<table>";
		htmlContent += "<tr>";
		htmlContent += "<td>姓名：</td><td>"+model.name+"</td>";
		htmlContent += "</tr>";
		htmlContent += "<tr>";
		htmlContent += "<td>单位：</td><td>"+model.unitName+"</td>";
		htmlContent += "</tr>";
		htmlContent += "<tr>";
		htmlContent += "<td>电话号码：</td><td>"+model.mobilephone+"</td>";
		htmlContent += "</tr>";
		htmlContent += "<tr>";
		htmlContent += "<td colspan='2'>";
		htmlContent += "<a target='_blank' style='margin-right: 5px;' href='"+burl+"/view/map/policeDispatch.jsp?no="+model.no+"&app="+overlay.data.APP+"'>实时轨迹</a>";
		htmlContent += "<a target='_blank' style='margin-right: 5px;' href='"+burl+"/view/map/policeReTrack.jsp?no="+model.no+"&app="+overlay.data.APP+"'>历史轨迹</a>";
		htmlContent += "<button telno='"+model.mobilephone+"' no='"+model.no+"' y='"+model.y+"' x='"+model.x+"' id='zl_btn'>指挥调度</button>";
		htmlContent += "</td>";
		htmlContent += "</tr><table>";
		var infoWindows = new BMap.InfoWindow(htmlContent);
		infoWindows.addEventListener("open",function(){
			$("#zl_btn").bind("click",function(){
				var x = $(this).attr("x");
				var y = $(this).attr("y");
				var no = $(this).attr("no");
				var telno = $(this).attr("telno");
				sendOrder.showOrderDialog(telno);
			});
			$("#gpsTraceBtn").bind("click",function(){
				var x = $(this).attr("x");
				var y = $(this).attr("y");
				var no = $(this).attr("no");
				boss.showOrderWindows(x,y,no);
			});
		});
			
		mapBoss.map.openInfoWindow(infoWindows,point);
	},
	showOrderWindows:function(x,y,no){
		var boss = this;
		var mapBoss = boss.mapBoss;
		var point =new BMap.Point(x,y);
		var htmlContent = "<table>";
		htmlContent += "<tr>";
		htmlContent += "<td>姓名：</td><td>李兰天</td>";
		htmlContent += "</tr>";
		htmlContent += "<tr>";
		htmlContent += "<td>指令信息：</td><td><input /></td>";
		htmlContent += "</tr>";
		htmlContent += "<tr>";
		htmlContent += "<td></td><td><button id='fs_btn'>发送</button></td>";
		htmlContent += "</tr><table>";
		var infoWindows = new BMap.InfoWindow(htmlContent); 
		
		infoWindows.addEventListener("open",function(){
			$("#fs_btn").bind("click",function(){
				alert("发送成功");
				infoWindows.close();
			});
		});
		mapBoss.map.openInfoWindow(infoWindows,point);
	}
};